package com.zlm.mall.product.web;

import com.zlm.mall.product.entity.CategoryEntity;
import com.zlm.mall.product.service.CategoryService;
import com.zlm.mall.product.vo.Catelog2Vo;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: 郑
 */
@Controller
public class indexController {

    @Autowired
    CategoryService categoryService;
    @Resource
    RedissonClient redissonClient;
    @Resource
    RedisTemplate<String,String> redisTemplate;

    @GetMapping({"/","/index.html"})
    public String index(Model model){
        //查询一级分类
        List<CategoryEntity> list = categoryService.getLevel1Categorys();
        model.addAttribute("categorys",list);
        return "index";
    }

    @ResponseBody
    @GetMapping("/index/catalog.json")
    public Map<String, List<Catelog2Vo>> getCategoryJson(){

        return categoryService.getCategoryJson();
    }

    @ResponseBody
    @GetMapping("/hello")
    public String hello(){

        return "hello";
    }

    /**
     * 保证一定能读到最新数据,修改期间是一个排他锁,读锁是共享锁
     * 写锁没释放读锁就必须等待
     * 写 +读 等待写锁释放
     * 写 +写 阻塞方式
     * 读 + 写
     * @return
     */
    @GetMapping("/write")
    @ResponseBody
    public String writeValue(){
        RReadWriteLock lock = redissonClient.getReadWriteLock("rw_lock");
        String s = "";
        //写锁
        RLock rLock = lock.writeLock();

        try {
            //改变数据加写锁,读数据加读锁
            rLock.lock();
            System.out.println("写锁成功.."+Thread.currentThread().getId());
            s = UUID.randomUUID().toString();
            Thread.sleep(3000);
            redisTemplate.opsForValue().set("writeValue",s);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
            System.out.println("写锁释放...."+Thread.currentThread().getId());
        }
        return s;
    }

    @GetMapping("/read")
    @ResponseBody
    public String readValue(){
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("rw_lock");
        //读锁
        RLock rLock = readWriteLock.readLock();
        String s = "";
        rLock.lock();
        try {
            System.out.println("读锁加锁成功.."+Thread.currentThread().getId());
            s = redisTemplate.opsForValue().get("writeValue");
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            rLock.unlock();
            System.out.println("读锁释放成功.."+Thread.currentThread().getId());
        }

        return s;
    }

    /**
     * 停车
     * @return
     * @throws InterruptedException
     */
    @GetMapping("/park")
    @ResponseBody
    public String park() throws InterruptedException {
        RSemaphore park = redissonClient.getSemaphore("park");
        //占一个车位
        park.acquire();

        return "ok";
    }

    @GetMapping("/gogo")
    @ResponseBody
    public String gogo(){
        RSemaphore park = redissonClient.getSemaphore("park");
        //释放车位
        park.release();
        return "ok..";
    }

    @GetMapping(value = "/lockDoor",produces = "application/json;charset-UTF-8")
    @ResponseBody
    public String lockDoor() throws InterruptedException {
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.trySetCount(5);
        //等待闭锁都完成
        door.await();

        return "锁门";
    }

    @GetMapping(value="/go/{num}",produces = "application/json;charset-UTF-8")
    @ResponseBody
    public String go(@PathVariable("num") Integer num){
        RCountDownLatch door = redissonClient.getCountDownLatch("door");
        door.countDown();//计数减一
        return num+"班级学生走完了";
    }

}
